package com.example.yzx.allriddle;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.example.yzx.allriddle.daomain.QApp;
import com.example.yzx.allriddle.utils.Db;

import java.util.ArrayList;
import java.util.List;

public class StarActivity extends AppCompatActivity {
    private String[] data;
    private String[] types=new String[500];
    private int[] uidlist=new int[500];
    private int[] idlist=new int[500];
    private int[] starlist=new int[500];
    private Db db = new Db(this, 1);
    private QApp app;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_star);
        //收藏引导
        ActionBar actionBar = getSupportActionBar();
        if(actionBar != null){
            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
        SQLiteDatabase sqLiteDb = db.getWritableDatabase();
        String sql2="CREATE TABLE  IF NOT EXISTS star(id integer primary key autoincrement,uid integer,starid integer, type varchar(60),FOREIGN KEY(uid)REFERENCES user(uid))";
        sqLiteDb.execSQL(sql2);
        sqLiteDb.close();
        init();
        app= (QApp) getApplication();
        ArrayAdapter<String> adapter=new ArrayAdapter<String>(StarActivity.this,R.layout.star,data);
        ListView listView=findViewById(R.id.listview);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent=new Intent(StarActivity.this,StarDetailActivity.class);
                int ids=starlist[i];
                int did=idlist[i];
                String styles=types[i];
                intent.putExtra("id",ids);
                intent.putExtra("type",styles);
                intent.putExtra("did",did);
                startActivity(intent);
            }
        });
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                this.finish(); // back button
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void init() {
        SQLiteDatabase sqLiteDbup = db.getReadableDatabase();
        Cursor c = sqLiteDbup.query("star", new String[]{"id", "uid","starid","type"}, null, null, null, null, null);
        boolean sign = true;
        int i=0;
        List<String> list=new ArrayList<>();
        while (c.moveToNext()) {
            Integer id = c.getInt(c.getColumnIndex("id"));
            String name = c.getString(c.getColumnIndex("uid"));
            Integer starid = c.getInt(c.getColumnIndex("starid"));
            String type = c.getString(c.getColumnIndex("type"));
            app= (QApp) getApplication();
            int uid=app.getserid();
            String uname="";
            if((uid+"").equals(name)){
                if(type.equals("xiaohua/text"))
                    uname="笑话";
                else if(type.equals("miyu/search"))
                    uname="谜语";
                else if(type.equals("jzw/search"))
                    uname="脑筋急转弯";
                idlist[i]=id;
                list.add("编号: "+id+"     类型为："+uname+"");
                uidlist[i]=id;starlist[i]=starid;types[i]=type;
                i++;
            }
        }
        data=new String[list.size()];
        for(int j=0;j<list.size();j++){
            data[j]=list.get(j);
        }
    }
}
